package com.simblog.oaknt.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.simblog.oaknt.domain.User;
import com.simblog.oaknt.mapper.UserMapper;
import com.simblog.oaknt.service.IUserService;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

	private final static Logger log = Logger.getLogger(UserServiceImpl.class);

	@Autowired
	private UserMapper mapper;

	public UserMapper getMapper() {
		return mapper;
	}

	public List<User> check(String account, String password) {
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("account", account);
		params.put("password", password);

		List<User> list = mapper.check(params);

		if (list == null) {
			return new ArrayList<User>();
		}

		return list;
	}

	public boolean unique(String key, String info) {

		Map<String, Object> params = new HashMap<String, Object>();

		params.put(key, info);

		List<User> list = mapper.query(params);

		if (list == null || list.size() == 0) {
			return true;
		} else {
			return false;
		}

	}

	@Override
	public User findUserByEmail(String email) {
		Map<String, Object> params = new HashMap<>();
		params.put("email", email);
		List<User> list = mapper.query(params);
		if (list != null && list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	@Override
	public void resetPassword(Long userId, String password) {
		User user = mapper.selectByPrimaryKey(userId);
		user.setPassword(password);
		mapper.updateByPrimaryKey(user);
	}

	@Cacheable(value="MyCache",key="'user_name='+#userName")
	public User selectByUserName(String userName) {
		log.info("从数据库查询userName={" + userName + "}的用户信息");
		return mapper.selectByUserName(userName);
	}

}
